package 力扣日常刷题.木22二月.第17天223;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/2/23
 * @description
 */
public class Demo143括号生成 {


    int n;
    List<String> ans = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        this.n = n;
        dfs(new StringBuilder(), 0, 0) ;
        return ans;
    }

    public void dfs(StringBuilder sb, int l, int r) {
        if(r > n || l > n || r > l) return;
        if (sb.length() == 2 * n) {
            ans.add(sb.toString());
            return;
        }


        sb.append("(");
        dfs(sb,l+1,r);
        sb.deleteCharAt(sb.length()- 1);
        sb.append(")");
        dfs(sb,l,r+1);
        sb.deleteCharAt(sb.length()- 1);
    }

}
